/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package doh.logic;

import java.util.Properties;

/**
 *
 * @author lucas
 */
public class GameInstance {

    String p1;
    String p2;
    int wins1;  //number of rounds won by player1
    int wins2;  //number of rounds won by player2

    int throw1;  //player1's throw
    int throw2; //playe2's throw

    int roundWinner;
    boolean roundWon = false;
    boolean p1go;
    boolean p2go;
    String winStr;


    static String[] throwsArray = {"Rock", "Paper", "Scissors"};
    
    public GameInstance(String p1, String p2)
    {
        this.p1 = p1;
        this.p2 = p2;
    }

    public Properties throwObject(String player, int t) {
        roundWon = false;
        Properties p = new Properties();
        p.setProperty("p1", p1);
        p.setProperty("p2", p2);

        if(player.equals(p1)){
            throw1 = t;
            p1go = true;
        } else {
            throw2 = t;
            p2go = true;
        }
        // add throw to DB

        if(p1go && p2go){
            // determine winner
            roundWinner = getWinner();
            winStr = getStatusMessage(roundWinner);
            
            if(wins1 == 2){
                // record win
            } else if (wins2 == 2){
                // record win
            }

            p.setProperty("status", winStr);
            if (wins1 == 2){
                p.setProperty("end", "1");
            } else if(wins2 == 2) {
                p.setProperty("end", "2");
            } else {
                p.setProperty("end", "false");
            }
            roundWon = true;
        }

        while(!roundWon);

        p1go=false;
        p2go=false;
        return p;
    }

    private int getWinner(){
        if(throw1 == 1){
            if(throw2 == 1) return 0;
            if(throw2 == 2) return 2;
            return 1;
        }
        if(throw1 == 2){
            if(throw2 == 1) return 1;
            if(throw2 == 2) return 0;
            return 2;
        }
        if(throw1 == 3){
            if(throw2 == 1) return 2;
            if(throw2 == 2) return 1;
            return 0;
        }
        return 0;
    }

    private String getStatusMessage(int winner){
        if(roundWinner == 1){ //p1
                wins1++;
                winStr = throwsArray[throw1] + " beats " + throwsArray[throw2];
            } else if(roundWinner == 2){ //p2
                wins2++;
                winStr = throwsArray[throw2] + " beats " + throwsArray[throw1];
            } else { //tie
                winStr = "TIE! throw again!";
            }
        return winStr;
    }
}
